



var db;







function createDB() {
    db = openDatabase("dbMap", "0.1", "A list of points.", 200000);
    if(!db){alert("Failed to connect to database.");}else{console.log('Data Base created');}
   
    db.transaction(function(tx) {
                   tx.executeSql("DROP TABLE mPins", [], null, null);
                   });

    
    db.transaction(function(tx) {
                  tx.executeSql("CREATE TABLE mPins (ident REAL UNIQUE, address TEXT, lat REAL,  longt REAL,name TEXT, type REAL, InstrumentTypeID REAL, SynchronisationFlag REAL, EventsFlag REAL,DiagnosticFlag REAL, OutlierFlag REAL, ProductLimitsFlag REAL, InstrumentPicture TEXT, SerialNumber TEXT, InstalledAtUTC TEXT, RegisteredAtUTC TEXT, GroupName TEXT, LastName TEXT, FirstName TEXT, OfficePhone TEXT, CellPhone TEXT, Email TEXT)", [], null, null);
                   });

    
};

function loadPins(fPath) {

    loadJSON(fPath,
             function(data) { console.log(data); },
             function(xhr) { console.error(xhr); }
             );

};

function deletePins() {
    
    if(!db){alert("Failed to connect to database.");return;}

    db.transaction(function(tx) {
                   tx.executeSql("DELETE FROM mPins", [], null, null)});

    clearPins();
};
function printPins() {
    if(!db){alert("Failed to connect to database.");return;}
    db.transaction(function(tx) {
                   tx.executeSql("SELECT * FROM mPins", [], function(tx, result) {
                                 for(var i = 0; i < result.rows.length; i++) {
                                    console.log('<b>' + result.rows.item(i)['label']+'</b><br />');
                                 }}, null)});



};
function loadJSON(path, success, error)
{
    if(!db){alert("Failed to connect to database.");return;}
    var request = new XMLHttpRequest();
    request.open("GET", path, false);
    request.send(null)
    var my_JSON_object = JSON.parse(request.responseText);
    var o=my_JSON_object.mPoint;
    db.transaction(function(tx) {
                   for (var p in o) {
        
                       tx.executeSql("INSERT INTO mPins (ident , address , lat , longt ,name , type,InstrumentTypeID , SynchronisationFlag , EventsFlag ,DiagnosticFlag , OutlierFlag , ProductLimitsFlag, InstrumentPicture , SerialNumber ,InstalledAtUTC , RegisteredAtUTC , GroupName ,LastName ,FirstName ,OfficePhone ,CellPhone ,Email) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [o[p].id, o[p].address, o[p].lat ,o[p].longt , o[p].name, o[p].type, o[p].InstrumentTypeID, o[p].SynchronisationFlag, o[p].EventsFlag ,o[p].DiagnosticFlag , o[p].OutlierFlag, o[p].ProductLimitsFlag, o[p].InstrumentPicture , o[p].SerialNumber ,o[p].InstalledAtUTC , o[p].RegisteredAtUTC , o[p].GroupName ,o[p].LastName ,o[p].FirstName ,o[p].OfficePhone ,o[p].CellPhone ,o[p].Email], null, function (tx, error) {
                                     console.log(error);
                                     });
                   
                   }
    
     });

};